package com.chenc.javathreads.uid.config;

import com.chenc.javathreads.uid.impl.CachedUidGenerator;
import com.chenc.javathreads.uid.impl.DefaultUidGenerator;
import com.chenc.javathreads.uid.worker.DisposableWorkerIdAssigner;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @ClassName
 * @Describtion:
 * @Author:Chenc
 * @Date:2023/12/28 16:04
 * @Version 1.0
 */
@Configuration
public class DefaultUidBeanConfig {

    @Resource
    CacheUidBeanConfig cacheUidBeanConfig;
//    @Bean(name = "disposableWorkerIdAssigner")
//    public DisposableWorkerIdAssigner getDisposableWorkerIdAssigner(){
//        return new DisposableWorkerIdAssigner();
//    }

    @Value("${uid.defaultUidGenerator.timeBit:29}")
    private int timeBit;
    @Value("${uid.defaultUidGenerator.workBit:21}")
    private int workBit;
    @Value("${uid.defaultUidGenerator.seqBit:13}")
    private int seqBit;
    @Bean(name = "defaultUidGenerator") //lazy-init="false"
    public DefaultUidGenerator getDefaultUidGenerator(){
        DefaultUidGenerator defaultUidGenerator = new DefaultUidGenerator();
        defaultUidGenerator.setWorkerIdAssigner(cacheUidBeanConfig.getDisposableWorkerIdAssigner());
        defaultUidGenerator.setTimeBits(timeBit);
        defaultUidGenerator.setWorkerBits(workBit);
        defaultUidGenerator.setSeqBits(seqBit);
        String now = LocalDate.now().atStartOfDay().toLocalDate().toString();
        defaultUidGenerator.setEpochStr(now);
        return defaultUidGenerator;
    }



}
